<template>
  <el-dialog :title="title" :visible="showDialog" @close="btnCancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form v-if="showDialog" ref="deptForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
          @focus="getEmployeeSimple"
          @blur="checkManager"
        >
          <el-option
            v-for="person in people"
            :key="person.id"
            :label="person.username"
            :value="person.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="addDepartments">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      required: true
    }
  },
  data() {
    const checkNameRepet = async(rule, value, callback) => {
      const res = await getDepartments()
      let isRepet
      if (this.formData.id) {
        isRepet = res.depts.filter(item => {
          return item.pid === this.treeNode.pid
        }).some(item => {
          return item.name === value && item.id === this.formData.id
        })
      } else {
        isRepet = res.depts.filter(item => {
          return item.pid === this.treeNode.id
        }).some(item => {
          return item.name === value
        })
      }
      isRepet = res.depts.filter(item => {
        return item.pid === this.treeNode.id
      }).some(item => {
        return item.name === value
      })
      if (isRepet) {
        callback(new Error(`同级部门下已经有${value}的部门了`))
      } else {
        callback()
      }
    }
    const checkCodeRepet = async(rule, value, callback) => {
      const res = await getDepartments()
      const isRepet = res.depts.some(item => {
        return item.code === value && item.id !== this.formData.id
      })
      if (isRepet) {
        callback(new Error(`组织架构中已经有部门使用${value}编码`))
      } else {
        callback()
      }
    }
    return {
      formData: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名字不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepet }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepet }
        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      people: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '编辑部门' : '增加部门'
    }
  },
  methods: {
    async addDepartments() {
      // console.log(this.formData, this.treeNode.id)
      try {
        await this.$refs.deptForm.validate()
        if (this.formData.id) {
          await updateDepartments(this.formData)
        } else {
          await addDepartments({ ...this.formData, pid: this.treeNode.id })
        }
        this.$emit('addDepts')
        this.$emit('update:showDialog', false)
        this.$message.success('操作成功')
      } catch (error) {
        // console.log(error)
        this.$message.error('添加失败')
      }
    },
    async getEmployeeSimple() {
      this.people = await getEmployeeSimple()
    },
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    },
    checkManager() {
      setTimeout(() => {
        this.$refs.deptForm.validateField('manager')
      }, 200)
    },
    btnCancel() {
      // 手动清空
      this.formData = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.deptForm.resetFields()
      this.$emit('update:showDialog', false)
    }
  }
}
</script>

<style>
</style>
